package org.glow.changgou.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.order.dao.ReturnOrderItemMapper;
import org.glow.changgou.order.pojo.OrderLog;
import org.glow.changgou.order.pojo.ReturnCause;
import org.glow.changgou.order.pojo.ReturnOrderItem;
import org.glow.changgou.order.service.ReturnOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:glow
 * @Description:ReturnOrderItem业务层接口实现类
 *****/
@Service
public class ReturnOrderItemServiceImpl extends ServiceImpl<ReturnOrderItemMapper,ReturnOrderItem>implements ReturnOrderItemService {

    @Resource
    private ReturnOrderItemMapper returnOrderItemMapper;


    /**
     * ReturnOrderItem条件+分页查询
     * @param returnOrderItem 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<ReturnOrderItem> findPage(ReturnOrderItem returnOrderItem, int page, int size){
        Page<ReturnOrderItem> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<ReturnOrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,returnOrderItem);
        return returnOrderItemMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * ReturnOrderItem分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ReturnOrderItem> findPage(int page, int size){
        Page<ReturnOrderItem> pageInfo = new Page<>(page,size);

        return returnOrderItemMapper.selectPage(pageInfo,null);
    }

    /**
     * ReturnOrderItem条件查询
     * @param returnOrderItem
     * @return
     */
    @Override
    public List<ReturnOrderItem> findList(ReturnOrderItem returnOrderItem){

        LambdaQueryWrapper<ReturnOrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,returnOrderItem);
        return returnOrderItemMapper.selectList(queryWrapper);
    }


    /**
     *
     * @param queryWrapper
     * @param returnOrderItem
     * @return
     */
    public LambdaQueryWrapper<ReturnOrderItem> getCondition(LambdaQueryWrapper<ReturnOrderItem> queryWrapper, ReturnOrderItem returnOrderItem){

        if(returnOrderItem!=null){
            // ID
            if(!StringUtils.isEmpty(returnOrderItem.getId())){
//                    criteria.andEqualTo("id",returnOrderItem.getId());
                    queryWrapper.eq(ReturnOrderItem::getId,returnOrderItem.getId());
            }
            // 分类ID
            if(!StringUtils.isEmpty(returnOrderItem.getCategoryId())){
//                    criteria.andEqualTo("categoryId",returnOrderItem.getCategoryId());
                    queryWrapper.eq(ReturnOrderItem::getCategoryId,returnOrderItem.getCategoryId());
            }
            // SPU_ID
            if(!StringUtils.isEmpty(returnOrderItem.getSpuId())){
//                    criteria.andEqualTo("spuId",returnOrderItem.getSpuId());
                    queryWrapper.eq(ReturnOrderItem::getSpuId,returnOrderItem.getSpuId());
            }
            // SKU_ID
            if(!StringUtils.isEmpty(returnOrderItem.getSkuId())){
//                    criteria.andEqualTo("skuId",returnOrderItem.getSkuId());
                    queryWrapper.eq(ReturnOrderItem::getSkuId,returnOrderItem.getSkuId());
            }
            // 订单ID
            if(!StringUtils.isEmpty(returnOrderItem.getOrderId())){
//                    criteria.andEqualTo("orderId",returnOrderItem.getOrderId());
                    queryWrapper.eq(ReturnOrderItem::getOrderId,returnOrderItem.getOrderId());
            }
            // 订单明细ID
            if(!StringUtils.isEmpty(returnOrderItem.getOrderItemId())){
//                    criteria.andEqualTo("orderItemId",returnOrderItem.getOrderItemId());
                    queryWrapper.eq(ReturnOrderItem::getOrderItemId,returnOrderItem.getOrderItemId());
            }
            // 退货订单ID
            if(!StringUtils.isEmpty(returnOrderItem.getReturnOrderId())){
//                    criteria.andEqualTo("returnOrderId",returnOrderItem.getReturnOrderId());
                    queryWrapper.eq(ReturnOrderItem::getReturnOrderId,returnOrderItem.getReturnOrderId());
            }
            // 标题
            if(!StringUtils.isEmpty(returnOrderItem.getTitle())){
//                    criteria.andLike("title","%"+returnOrderItem.getTitle()+"%");
                    queryWrapper.like(ReturnOrderItem::getTitle,returnOrderItem.getTitle());
            }
            // 单价
            if(!StringUtils.isEmpty(returnOrderItem.getPrice())){
//                    criteria.andEqualTo("price",returnOrderItem.getPrice());
                    queryWrapper.eq(ReturnOrderItem::getPrice,returnOrderItem.getPrice());
            }
            // 数量
            if(!StringUtils.isEmpty(returnOrderItem.getNum())){
//                    criteria.andEqualTo("num",returnOrderItem.getNum());
                    queryWrapper.eq(ReturnOrderItem::getNum,returnOrderItem.getNum());
            }
            // 总金额
            if(!StringUtils.isEmpty(returnOrderItem.getMoney())){
//                    criteria.andEqualTo("money",returnOrderItem.getMoney());
                    queryWrapper.eq(ReturnOrderItem::getMoney,returnOrderItem.getMoney());
            }
            // 支付金额
            if(!StringUtils.isEmpty(returnOrderItem.getPayMoney())){
//                    criteria.andEqualTo("payMoney",returnOrderItem.getPayMoney());
                    queryWrapper.eq(ReturnOrderItem::getPayMoney,returnOrderItem.getPayMoney());
            }
            // 图片地址
            if(!StringUtils.isEmpty(returnOrderItem.getImage())){
//                    criteria.andEqualTo("image",returnOrderItem.getImage());
                    queryWrapper.eq(ReturnOrderItem::getImage,returnOrderItem.getImage());
            }
            // 重量
            if(!StringUtils.isEmpty(returnOrderItem.getWeight())){
//                    criteria.andEqualTo("weight",returnOrderItem.getWeight());
                    queryWrapper.eq(ReturnOrderItem::getWeight,returnOrderItem.getWeight());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        returnOrderItemMapper.deleteById(id);
    }

    /**
     * 修改ReturnOrderItem
     * @param returnOrderItem
     */
    @Override
    public void update(ReturnOrderItem returnOrderItem){
        returnOrderItemMapper.updateById(returnOrderItem);
    }

    /**
     * 增加ReturnOrderItem
     * @param returnOrderItem
     */
    @Override
    public void add(ReturnOrderItem returnOrderItem){
        returnOrderItemMapper.insert(returnOrderItem);
    }

    /**
     * 根据ID查询ReturnOrderItem
     * @param id
     * @return
     */
    @Override
    public ReturnOrderItem findById(Long id){
        return  returnOrderItemMapper.selectById(id);
    }

    /**
     * 查询ReturnOrderItem全部数据
     * @return
     */
    @Override
    public List<ReturnOrderItem> findAll() {
        return returnOrderItemMapper.selectList(null);
    }
}
